package com.example.studentregistration.controller;

import com.example.studentregistration.pojo.DormRoom;
import com.example.studentregistration.service.DormRoomService;
import com.example.studentregistration.util.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api")
public class DormRoomController {

    @Autowired
    private DormRoomService dormRoomService;

    // 新增房间
    @PostMapping("/dormRoom/add")
    public Result<?> add(@RequestBody DormRoom room) {
        return dormRoomService.insert(room);
    }

    // 修改房间
    @PutMapping("/dormRoom/update")
    public Result<?> update(@RequestBody DormRoom room) {
        return dormRoomService.update(room);
    }

    // 删除房间
    @DeleteMapping("/dormRoom/delete/{id}")
    public Result<?> delete(@PathVariable Integer id) {
        return dormRoomService.delete(id);
    }

    // 批量删除
    @PostMapping("/dormRoom/batchDelete")
    public Result<?> batchDelete(@RequestBody List<Integer> ids) {
        if (ids == null || ids.isEmpty()) {
            return Result.fail("请选择要删除的记录");
        }
        int result = dormRoomService.batchDelete(ids);
        if (result > 0) {
            return Result.success("删除成功" + result + "条记录");
        }
        return Result.fail("删除失败");
    }

    // 分页查询
    @GetMapping("/dormRoom")
    public Result<PageInfo<Object>> queryAll(@RequestParam(defaultValue = "1") Integer currentPage,
                                             @RequestParam(defaultValue = "10") Integer pageSize) {
        Page page = dormRoomService.queryAll(currentPage, pageSize);
        return Result.success(new PageInfo<>(page));
    }

    // 根据ID查询
    @GetMapping("/dormRoom/queryById/{id}")
    public Result<DormRoom> queryById(@PathVariable Integer id) {
        return dormRoomService.queryById(id);
    }

    // 根据楼宇ID查询房间
    @GetMapping("/dormRoom/queryByBuilding")
    public Result<List<DormRoom>> queryByBuilding(@RequestParam Integer buildingId) {
        return dormRoomService.queryByBuildingId(buildingId);
    }

    // 新增：导出房间Excel
    @GetMapping("/dormRoom/export")
    public void export(HttpServletResponse response) throws IOException {
        dormRoomService.exportExcel(response);
    }

    // 新增：导入房间Excel
    @PostMapping("/dormRoom/import")
    public Result<?> importExcel(@RequestParam("file") MultipartFile file) throws IOException {
        ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
        List<DormRoom> list = reader.readAll(DormRoom.class);
        int success = 0;
        for (DormRoom item : list) {
            if (item == null) continue;
            Result<?> r = dormRoomService.insert(item);
            if (r.getCode() == 200) success++;
        }
        return Result.success("导入成功" + success + "条");
    }
}